package club.xyes.zkh.ecommerce.web.controller;

import club.xyes.zkh.ecommerce.commons.entity.BaseEntity;
import club.xyes.zkh.ecommerce.commons.entity.Shop;
import club.xyes.zkh.ecommerce.commons.entity.User;
import club.xyes.zkh.ecommerce.commons.properties.WebsiteConfigurationProperties;
import club.xyes.zkh.ecommerce.commons.util.EntityImagePathUtils;
import club.xyes.zkh.ecommerce.commons.vo.GeneralResult;
import club.xyes.zkh.ecommerce.service.BaseService;
import club.xyes.zkh.ecommerce.service.ShopService;
import club.xyes.zkh.ecommerce.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * Create by 郭文梁 2019/4/15 0015 15:24
 * BaseController
 * 控制器基类
 *
 * @author 郭文梁
 * @data 2019/4/15 0015
 */
public abstract class BaseController<T extends BaseEntity> extends AbstractControllerSupport {
    /**
     * 分页 默认页数
     */
    protected static final int DEFAULT_PAGE = 1;
    /**
     * 分页 默认每页大小
     */
    protected static final int DEFAULT_ROWS = 10;
    private final BaseService<T> service;

    @Autowired
    public BaseController(BaseService<T> service) {
        this.service = service;
    }

    /**
     * 分页查询所有试题对象
     *
     * @param page 页码
     * @param rows 每页大小
     * @return GeneralResult with pageinfo object
     */
    @GetMapping("/")
    @ApiOperation(value = "分页查询所有对象", notes = "分页查询所有试题对象")
    public GeneralResult<PageInfo<T>> all(Integer page, Integer rows) {
        if (page == null) {
            page = DEFAULT_PAGE;
        }
        if (rows == null) {
            rows = DEFAULT_ROWS;
        }
        PageInfo<T> pageInfo = service.list(page, rows);
        return GeneralResult.ok(pageInfo);
    }

    /**
     * 根据ID查询对象
     *
     * @param id ID
     * @return GeneralResult with entity
     */
    @GetMapping("/{id:\\d+}")
    @ApiOperation("根据ID查询")
    public GeneralResult<T> get(@PathVariable("id") Integer id) {
        return GeneralResult.ok(service.getRequired(id));
    }

    /**
     * 获取当前登录的用户
     *
     * @param userService 用户业务组件
     * @return 用户信息
     */
    protected User currentUser(UserService userService) {
        Integer userId = getRequiredUserInfo().getUserId();
        return userService.getRequired(userId);
    }

    /**
     * 获取当前登录的商铺对象
     *
     * @param shopService 商铺业务组件
     * @return 商铺
     */
    protected Shop currentShop(ShopService shopService) {
        Integer shopId = getRequiredShopInfo().getShopId();
        return shopService.getRequired(shopId);
    }

    /**
     * 替换商铺图片路径
     *
     * @param websiteConfigurationProperties 配置
     * @param shop                           商铺
     */
    protected void prettyPath(WebsiteConfigurationProperties websiteConfigurationProperties, Shop shop) {
        String serverPrefix = websiteConfigurationProperties.getStaticServerPrefix();
        String promotionImagePath = websiteConfigurationProperties.getShopPromotionImagePath();
        String logoPath = websiteConfigurationProperties.getShopLogoPath();
        EntityImagePathUtils.prettyLogoPath(shop, serverPrefix, logoPath);
        EntityImagePathUtils.prettyShopPromotionImagePath(shop, serverPrefix, promotionImagePath);
    }
}
